package com.ys.controller.api.business;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.business.TpProductActiveSq;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.TpProductActiveSqService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tpProductActiveSq")
public class ApiInterTpProductActiveSqController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTpProductActiveSqController.class);


    @Autowired
    private CommonApiService<TpProductActiveSq> commonApiService;

    @Autowired
    private TpProductActiveSqService tpProductActiveSqService;

    @RequiresPermissions("tpProductActiveSq:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TpProductActiveSq cond){
        cond.addLeftJoin("YS_ProcInst C ON  C.Bid = A.Bid");
        cond.addLeftJoin("TS_UserChange t2 on  A.CreateChangeId = t2.id");
        cond.addField("t2.UserName as createUserInfo, t2.DeptName as creatUserDept,t2.RoleName as createPostName," +
                "t2.UserName as createUserNameTxt,t2.DeptName as createDeptName");
        cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.VgsBm");
        cond.addField("THRO.CUser AS cUser,THRO.VGsJc vgsJc,THRO.VGsMc vgsMc");
        cond.addField("C.UpdateTime updateTime1");
        cond.setOrderby("A.createTime DESC");
        cond = commonApiService.dataAuth(MenuId.TP_ProductActiveSq, cond);
        return commonApiService.searchLay(cond);
    }


    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params, String subData) {
        try {
            logger.info("申请新增传参：" + params);
            return tpProductActiveSqService.add(params,subData);
        } catch (Exception e) {
            logger.error("新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData) {
        try {
            logger.info("申请修改传参：" + params);
            //修改
            return tpProductActiveSqService.update(params,subData);
        } catch (Exception e) {
            logger.error("申请出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 删除申请
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(Long id){
        try {
            return tpProductActiveSqService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败，请联系客服");
        }
    }



    /**
     * 流转通过
     */
    @PostMapping("/flowPass")
    @ResponseBody
    public R flowPass(HttpServletRequest request, Long id){
        try {
            return tpProductActiveSqService.flowPass(id, ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }

    /**
     * 流转驳回
     */
    @PostMapping("/flowRefuse")
    @ResponseBody
    public R flowRefuse(HttpServletRequest request, Long id, String reason){

//        TpProductActiveSq entity = new TpProductActiveSq().setId(id).queryFirst();
//        Long id = entity.getId();
        try {
            return tpProductActiveSqService.flowRefuse(id, reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("服务器正在开小差，请联系客服");
        }
    }


    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TpProductActiveSq cond, String conditions) throws Exception{

        cond.setPage(0).setSize(999999);
        cond.addLeftJoin("YS_ProcInst C ON  C.Bid = A.Bid");
        cond.addLeftJoin("TS_UserChange t2 on  A.CreateChangeId = t2.id");
        cond.addField("t2.UserName as createUserInfo, t2.DeptName as creatUserDept,t2.RoleName as createPostName");
        cond.addField("C.UpdateTime updateTime1");
        cond.setOrderby("A.createTime DESC");

        List<Map<String, Object>> listData = cond.queryMap();


        List<YsCol> listField = new YsCol()
                .setMenuId(MenuId.TP_ProductActiveSq)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();

        ExportExcelUtils<TpProductActiveSq> util = new ExportExcelUtils<TpProductActiveSq>(new TpProductActiveSq());

        String toFilePath = util.exportExcel(listData, listField, "申请表");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }
}
